package cn.itsource.service.impl;

import cn.itsource.constants.Constants;
import cn.itsource.desktop.api.LoginApi;
import cn.itsource.desktop.pojo.param.LoginFound;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.pojo.dto.WxRegisterDTO;

import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.ICustomerSummaryService;
import cn.itsource.service.ICustomerWalletService;
import cn.itsource.template.WxTemplate;
import cn.itsource.utils.AssertUtil;
import cn.itsource.utils.BitStatesUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author ????
 * @since 2024-06-15
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {


    @Autowired
    private ICustomerWalletService walletService;

    @Autowired
    private ICustomerSummaryService summaryService;

    @Autowired
    private LoginApi loginApi;

    @Autowired
    private WxTemplate wxTemplate ;

    /**
     * 微信注册逻辑
     * 1.获取参数：code,小程序ID，小程序秘钥
     * 2.拼接一URL，组装参数，发送Http请求(RestTemplate)，
     * 3.拿到结果，获取OpenId
     * 4.判断：OpenId是否已经存在，如果存在就提示已经注册
     * 5.OpenId不存在：保存司机对象：初始化关联对象：钱包，设置，结算
     * 6.远程保存Login对象
     *
     * @param dto ：参数对象，包括微信code
     */

    @Override
    @Transactional//开启事务
    public void wxRegister(WxRegisterDTO dto) {
        //* 1.根据：获取参数：code,小程序ID，小程序秘钥 ，获取微信ID
        String openId = wxTemplate.getOpenId(dto.getWxCode());

        //2.判断是否已经注册
        Customer customer = selectCustomerByOpenId(openId);
        AssertUtil.isNull(customer,"您已注册");

        //获取手机号=======================================================
        String phone = wxTemplate.getPhone(dto.getWxCode());

        //* 3.OpenId不存在：保存司机对象：初始化关联对象：钱包，设置，结算
        customer = createCustomer(openId,phone);
        walletService.createWallet(customer.getId());
        summaryService.createSummary(customer.getId());
        //* 6.远程保存Login对象
        createLogin(customer);
    }

    //根据OpenId查询乘客
    private Customer selectCustomerByOpenId(String openId) {
        //* 4.判断：OpenId是否已经存在，如果存在就提示已经注册
        LambdaQueryWrapper<Customer> query = new LambdaQueryWrapper<>();
        //条件：whre open_id = #{openId}
        query.eq(Customer::getOpenId, openId);
        //根据OpenId查询乘客
        Customer customer = super.getOne(query);
        return customer;
    }

    //保存Login
    private void createLogin(Customer customer){

        LoginFound login = LoginFound.builder()
                .phone(customer.getPhone())
                .openId(customer.getOpenId())
                //乘客类型
                .type(Constants.Driver.TYPE_CUSTOMER)
                .username(customer.getPhone())
                .id(customer.getId())
                .build();

        JSONResult jsonResult = loginApi.create(login);
        AssertUtil.isTrue(jsonResult.isSuccess(),"远程保存Login失败");
    }


    //保存乘客
    private Customer createCustomer(String openid,String phone) {
        Customer customer = new Customer();
        customer.setOpenId(openid);
        customer.setPhone(phone);
        customer.setCreateTime(new Date());
        //给司机添加手机认证的位状态
        long bitState = BitStatesUtils.addState(0L, BitStatesUtils.OP_PHONE);
        customer.setBitState(bitState);

        super.save(customer);
        return customer;
    }

}
